<?php
namespace App\Models;

use App\Utils\RolesUtil;

/**
 * 系统公告对象
 */
class MPermissionRoles extends BaseModel
{
    public $table = "permission_role";

    //表关系区===========================================================================

    //逻辑处理区===========================================================================
    public static function getAdminPermission($role_id)
    {
        $permission_role = MPermissionRoles::where('role_id', $role_id)->first();

        if ($permission_role) {
            $permissions = $permission_role->permissions;
        } else {
            $permissions = serialize([]);
        }

        return unserialize($permissions);
    }

    //遍历出数据库中的权限
    public static function getPermission($role_id)
    {
        $permission_role = MPermissionRoles::where('role_id', 9)->first();
        if ($permission_role) {
            $permissions = unserialize($permission_role->permissions);
            $array = [];
            foreach ($permissions as $value) {
                dd($value['id']);
                $res['id'] = $value['id'];
                $res['title'] = $value['title'];
                if (in_array($res['id'], $permissions)) {
                    $res['checked'] = true;
                }else{
                    $res['checked'] = false;
                }
                $goods = [];
                foreach ($value['child'] as $child) {
                    foreach ($child['child'] as $grandson) {
                        $data1['id'] = $grandson['id'];
                        $data1['title'] = $grandson['id'];
                        if (in_array($data1['id'], $permissions)) {
                            $data1['checked'] = true;
                        }else{
                            $data1['checked'] = false;
                        }
                        $goods[] = $data1;
                    }
                }
                $res['grandson'] = $goods;
                $array[] = $res;
            }
        } else {
            $array = [];
        }
        return $array;
    }

}